<?php

namespace common\entities\official;

use common\entities\ContractEntity;

class DirEntity extends ContractEntity
{

    /**
     * 'home_banner'=>[],//首页banner
     * 'page_banner'=>[],//二级页banner
     * 'recommend'=>[],//热门推荐
     * 'study'=>[],//新手学习
     * 'product'=>[],//产品直达
     * 'notice'=>[],//动态公告
     * 'download'=>[],//藏宝阁
     * 'college'=>[],//翰林院
     */
    public static $enumCode = ['home_banner', 'page_banner', 'recommend', 'study', 'product', 'notice', 'download', 'college', 'media'];

    public static function tableName()
    {
        return 'myscrm_official_dir';
    }

    public function rules()
    {
        return [
            [['parent_id', 'name', 'sort'], 'required'],
            [['parent_id', 'sort', 'status', 'external_display'], 'integer'],
            [['code', 'name'], 'string', 'max' => 30],
            [['short_desc', 'img_url', 'url'], 'string', 'max' => 255],
            [['code'], 'unique'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => '编号',
            'parent_id' => '上级目录',
            'code' => '编码',
            'name' => '名称',
            'short_desc' => '简要描述',
            'img_url' => '图片地址',
            'url' => '跳转地址',
            'sort' => '排序',
            'status' => '发布状态',
            'external_display' => '对外显示'
        ];
    }

    public function getParent()
    {
        return $this->hasOne(__CLASS__, ['id' => 'parent_id']);
    }

    public function getChildren()
    {
        return $this->hasMany(__CLASS__, ['parent_id' => 'id'])->where([])->orderBy('sort DESC');
    }

    protected function getDepth()
    {
        $depth = 0;
        $self = $this;
        while (!is_null($self->parent)) {
            ++$depth;
            $self = $self->parent;
        }
        return $depth;
    }

    protected function getIsLeaf()
    {
        return !self::find()->where(['parent_id' => $this->id])->count() > 0;
    }

    public function getDirList($where)
    {
        $query = self::find();
        if (!empty($where['parent_id'])) {
            $query->andWhere(['=', 'parent_id', $where['parent_id']]);
        } else {
            $query->andWhere(['=', 'parent_id', 0]);
        }
        return $query->orderBy('sort DESC')->all();
    }

    public static function getOptions($id = 0)
    {
        static $level = 0;
        $id = "" == $id ? 0 : $id;
        $items = array();
        $where = [];
        if (is_numeric($id)) {
            $where = ['=', 'parent_id', $id];
        } else {
            $where = ['=', 'code', $id];
        }
        $models = self::find()->where($where)->orderBy('sort DESC')->all();
        foreach ($models as $model) {
            $items[$model->primaryKey] = str_repeat("--", $level) . $model->name;
            if (!$model->isLeaf) {
                ++$level;
                $items += self::getOptions($model->primaryKey);
                --$level;
            }
        }
        return $items;
    }

    public static function getEnumOption()
    {
        $ret = [];
        foreach (self::$enumCode as $value) {
            $ret[$value] = $value;
        }
        return $ret;
    }

    /**
     * 只返回二级目录
     */
    public static function getDir()
    {
        $ret = [];
        $models = self::find()->where('status=1 and parent_id=0')->orderBy('sort DESC')->all();
        if (!empty($models)) {
            foreach ($models as $item) {
                $children = [];
                if ($item->children) {
                    foreach ($item->children as $item2) {
                        $children[] = ['id' => $item2->id, 'name' => $item2->name, 'short_desc' => $item2->short_desc, 'img_url' => $item2->img_url, 'url' => $item2->url];
                    }
                }
                $ret[$item->code] = ['id' => $item->id, 'name' => $item->name, 'short_desc' => $item->short_desc, 'img_url' => $item->img_url, 'url' => $item->url, 'children' => $children];
            }
        }
        return $ret;
    }

}
